#import <UIKit/UIKit.h>
#import <UIKit/UIControl.h>
#import <UIKit/UIFont.h>
#import <UIKit/UIColor.h>
// #import <UIKit/UIGeometry.h>	// UIEdgeInsetsMaek()

#import "TVPanel.h"
#import "QtKey.h"

typedef enum
{
	UIButtonTypeCustom = 0,
	UIButtonTypeRoundedRect,
	UIButtonTypeDetailDisclosure,
	UIButtonTypeInfoLight,
	UIButtonTypeInfoDark,
	UIButtonTypeContactAdd,
} UIButtonType;

enum
{
	UIControlEventTouchDown           = 1 <<  0,
	UIControlEventTouchDownRepeat     = 1 <<  1,
	UIControlEventTouchDragInside     = 1 <<  2,
	UIControlEventTouchDragOutside    = 1 <<  3,
	UIControlEventTouchDragEnter      = 1 <<  4,
	UIControlEventTouchDragExit       = 1 <<  5,
	UIControlEventTouchUpInside       = 1 <<  6,
	UIControlEventTouchUpOutside      = 1 <<  7,
	UIControlEventTouchCancel         = 1 <<  8,
	
	UIControlEventValueChanged        = 1 << 12,
	
	UIControlEventEditingDidBegin     = 1 << 16,
	UIControlEventEditingChanged      = 1 << 17,
	UIControlEventEditingDidEnd       = 1 << 18,
	UIControlEventEditingDidEndOnExit = 1 << 19,
	
	UIControlEventAllTouchEvents      = 0x00000FFF,
	UIControlEventAllEditingEvents    = 0x000F0000,
	UIControlEventApplicationReserved = 0x0F000000,
	UIControlEventSystemReserved      = 0xF0000000,
	UIControlEventAllEvents           = 0xFFFFFFFF
};

enum
{
	UIControlStateNormal               = 0,
	UIControlStateHighlighted          = 1 << 0,
	UIControlStateDisabled             = 1 << 1,
	UIControlStateSelected             = 1 << 2,
	UIControlStateApplicationReserved  = 0x00FF0000,
	UIControlStateReserved             = 0xFF000000
};

@class UIButton;

@implementation TVPanel

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
	if ((self = [super initWithFrame:frame]) == nil)	return self;
	
	delegate = nil;
	
	keySets = [[NSMutableDictionary alloc] initWithCapacity:8];
	
	[self setBackgroundColor:[UIColor blackColor]];
	
	[self addSubview:[self buttonWithFrame:CGRectMake(120, 0, 80, 60) title:@"I/O" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F14], nil]]]];
	
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 60, 80, 60) title:NSLocalizedString(@"Menu", nil) keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F16], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 60, 80, 60) title:@"EPG" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F20], nil]]]];
	
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 0, 60, 60) title:@"+" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_Up], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 60, 60, 60) title:@"-" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_Down], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(220, 0, 40, 60) title:@"X" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F15], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(260, 0, 60, 60) title:@"+" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_Up], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(260, 60, 60, 60) title:@"-" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_Down], nil]]]];
	
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 120, 100, 60) title:@"1" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F17], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(110, 120, 100, 60) title:@"2" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F18], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(220, 120, 100, 60) title:@"3" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F19], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 180, 100, 60) title:@"4" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F20], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(110, 180, 100, 60) title:@"5" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F21], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(220, 180, 100, 60) title:@"6" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F22], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 240, 100, 60) title:@"7" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F23], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(110, 240, 100, 60) title:@"8" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Shift], [NSNumber numberWithInt:Key_F24], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(220, 240, 100, 60) title:@"9" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F13], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 300, 100, 60) title:@"10" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F16], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(110, 300, 100, 60) title:@"11" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F14], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(220, 300, 100, 60) title:@"12" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F15], nil]]]];
	
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 360, 80, 40) title:NSLocalizedString(@"Memo", nil) keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F23], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 360, 80, 40) title:@"●" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F22], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 360, 80, 40) title:@"■" keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F24], nil]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(240, 360, 80, 40) title:NSLocalizedString(@"Full", nil) keyArray:
			[NSArray arrayWithObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:Key_Control], [NSNumber numberWithInt:Key_F17], nil]]]];
	
	return self;
}

- (void)dealloc
{
	[super dealloc];
}

- (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title keyArray:(NSArray *)keys
{
	UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
	// [b setRotationBy:90.0];
	[b setFrame:frame];
	[b setShowsTouchWhenHighlighted:YES];
	[b setBackgroundColor:[UIColor blackColor]];
	[b setFont:[UIFont systemFontOfSize:28.0]];
	// [b setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[b setBackgroundImage:[UIImage imageNamed:@"Key.png"] forState:UIControlStateNormal];
	[b setTitle:title forState:UIControlStateNormal];
	[b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
	[b addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside];
	[b addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpOutside];
	
	[keySets setObject:keys forKey:[NSNumber numberWithInt:b]];
	
	return b;
}

- (void)buttonPressed:(UIButton *)button
{
	if ([[NSUserDefaults standardUserDefaults] boolForKey: @"enable_click_sound"])
		AudioServicesPlaySystemSound(1105);
	
	NSArray *keyCodes;
	if ((keyCodes = (NSArray *)[keySets objectForKey:[NSNumber numberWithInt:button]]) == nil)	return;
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex:keys];
		for (int set = 0; set != [sets count]; ++set)
		{
			int keyCode = [[sets objectAtIndex:set] intValue];
			[delegate qtKeyPressed:keyCode];
		}
	}
}

- (void)buttonReleased:(UIButton *)button
{
	NSArray *keyCodes;
	if ((keyCodes = (NSArray *)[keySets objectForKey:[NSNumber numberWithInt:button]]) == nil)	return;
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex:keys];
		for (int set = ([sets count] - 1); 0 <= set; --set)
		{
			int keyCode = [[sets objectAtIndex:set] intValue];
			[delegate qtKeyReleased:keyCode];
		}
	}
}

@end
